<?php

namespace App\Exports;

use App\Models\HousingsComment;
use Maatwebsite\Excel\Concerns\FromCollection;
use Maatwebsite\Excel\Concerns\WithHeadings;
use Maatwebsite\Excel\Concerns\WithMapping;


class HousingsCommentExport implements FromCollection,WithHeadings,WithMapping
{
    /**
    * @return \Illuminate\Support\Collection
    */

    public function collection()
    {
        $query=HousingsComment::query()->with(['user','housings','housingsorder']);
        if (isset($this->filters['ids'])) {
            $query->whereIn('id', explode(',',$this->filters['ids']));
        }
        return $query->get();
    }
    public function headings(): array
    {
        return [
            '排房订单号',
            '房源码',
            '房屋地址',
            '评价人',
            '联系电话',
            '评价结果',
            '评价时间',
            // ... 其他列标题
        ];
    }

    public function map($enterlog): array
    {
        // 假设订单有 id 和 total 字段
        $orderData[] = [
            $enterlog['sn']=isset($enterlog->housingsorder['sn']) ? $enterlog->housingsorder['sn'] :'',
            $enterlog['housings_num']=isset($enterlog->housings['housings_num']) ? $enterlog->housings['housings_num'] :'',
            $enterlog['hou_address']=isset($enterlog->housings['hou_address']) ? $enterlog->housings['hou_address'] :'',
            $enterlog['phone_number']=isset($enterlog->user['phone_number']) ? $enterlog->user['phone_number'] :'',

            $enterlog['real_name']=isset($enterlog->user['real_name']) ? $enterlog->user['real_name'] :'',
            $enterlog['comment_type'],
            $enterlog['created_at'],
        ];
        // 展开订单数据，以便每行都有用户信息和对应的订单信息
        return array_merge(...$orderData);
    }
}
